This lab will introduce us to separate files and the make command. These are vital for quick and efficient C++ programming.
Most C++ programs are modularized. If a procedural programming approach is used, the program may be formed of multiple functions and a driver. When an object-oriented programming approach is used, a program may contain multiple classes. To handle this, multiple files are used for organization.
Contain the declarations for the function names (in procedural programming) and classes (in object oriented programming). These have a .h
or .hpp
extension. Often times for functions you will have
return_type function_name(type, type, ...); // semicolon very important here
/* average.hpp - header file for average function declaration */
double average(int, double); OR
double average(int num_scores, double sum); // the variables local to the function are optional
Contain the actual definitions of the functions and classes. Generally these have a .cpp
file extension or .cc
.
/* average.cpp - average function definition */
//Need to use an include of the declarations, use quotes since YOU made the file *not a system file*
// Don't use cin or cout, so no iostream include necessary. If you don't use it in this file, DON'T INCLUDE IT!!
#include "average.hpp"
double average(int num_scores, double sum) // NO SEMICOLON HERE!
{ /* computes the average using an already computed sum and num_scores */
return sum / num_scores;
}
Contains the main function and is used to "drive" or test the other functions and classes declared and defined in other files.
/* avg_main.cpp - used to test the average function use */ #include <iostream> //necessary for cin/cout #include "average.hpp" // use the .hpp for includes using namespace std; int main(void) { /* Call and test all of the functions written */ cout << "Welcome to the average driver program.\n"; cout << "Please enter the number of items to be summed: "; int n, count=0;
double single_score, sum=0; cin >> n; while (count < n) // read in the test scores and sum them { cout << "Test score " << count + 1 << ":"; cin >> single_score; sum += single_score; count++; } // Call the average function double avg = average(n, sum); cout << "The average of " << sum << "/" << n << " is: " << avg << endl; return 0; }
Log on to loki.cs.kent.edu using ssh and your llname account. Change directories (cd) to your cs23021 folder. Create a directory named LnameLab7 by using mkdir LnameLab7.
Create the three files as listed above for the average function (average.hpp, average.cpp, avg_main.cpp
).
Compile these files using the following commands.
g++ -Wall -c avgerage.cpp
g++ -Wall -c avg_main.cpp
g++ -Wall average.o avg_main.o -o avg
To run the program, type in ./avg
Once you have the files working from the above Exercise, create a new function declaration called sum
in the average.hpp. It should take in one integer parameter that is the number of scores to be summed. The function should return a double.
Create a new function definition for sum
that in the file average.cpp. sum
should take the integer value that is passed to it, and in a loop read in and sum that many double values. The sum of these values should be returned. Hint: use the code from Exercise 7.1 as a guide. Be sure to include any files you may need as well as to use the namespace std.
To compile the average.cpp file and check for errors, use the command:
g++ -Wall -c avgerage.cpp
This does not produce an executable ("runnable") program - it compiles and creates an object .o
file, checking for errors.
Copy and use the following driver program to test your newly written function and save it as avg_driver.cpp:
/* avg_driver.cpp - used to test the average functions use */ #include <iostream> //necessary for cin/cout #include "average.hpp" // use the .hpp for includes using namespace std; int main(void) { /* Call and test all of the functions written */ cout << "Welcome to the average driver program.\n"; cout << "Please enter the number of items to be summed: "; int n; cin >> n; // Call the sum function to input and sum n numbers // Store the returned value in the variable sigma double sigma = sum(n); // call to your sum function // Call the average function double avg = average(n, sigma); cout << "The average of " << sigma << "/" << n << " is: " << avg << endl; return 0; }To compile and run the entire program, use the following four commands:
g++ -Wall -c average.cpp
g++ -Wall -c avg_driver.cpp
g++ -Wall avg_driver.o average.o -o avg
./avg